﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using VietImage.Models;
using VietImage.Roles;
using System.Windows.Threading;
using System.Threading;

namespace VietImageSoftware
{
	/// <summary>
	/// Interaction logic for ChooseParentCategoriesForm.xaml
	/// </summary>
	public partial class ChooseParentCategoriesForm : Window
	{
        private RegisteredUserRole userRole;

        public RegisteredUserRole UserRole
        {
            get { return userRole; }
            set { userRole = value; }
        }

        private Category currentCategory;

        public Category CurrentCategory
        {
            get { return currentCategory; }
            set { currentCategory = value; }
        }

        private List<Category> parentCategories;

        public List<Category> ParentCategories
        {
            get { return parentCategories; }
            set { parentCategories = value; }
        }

		public ChooseParentCategoriesForm()
		{
			this.InitializeComponent();
			
			// Insert code required on object creation below this point.
		}


        #region Choose Parent Categories

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            lbCPCParentCategories.Items.Clear();
            lbCPCAvailableCategories.Items.Clear();
            List<Category> l = currentCategory.GetAvailableParentCategories();
            if (l != null)
            {
                foreach (Category c in l)
                    lbCPCParentCategories.Items.Add(c.GetDetail(userRole.ID_Language));
                lbCPCParentCategories.SelectedValuePath = "ID_Category";
                lbCPCParentCategories.DisplayMemberPath = "Name";
            }
            l = currentCategory.GetAvailableNotParentCategories();
            if (l != null)
            {
                foreach (Category c in l)
                    lbCPCAvailableCategories.Items.Add(c.GetDetail(userRole.ID_Language));
                lbCPCAvailableCategories.SelectedValuePath = "ID_Category";
                lbCPCAvailableCategories.DisplayMemberPath = "Name";
            }
        }

        private void btnCPCChoose_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            MessageForm form = new MessageForm();
            form.UserRole = userRole;
            form.HaveOKButton = true;
            form.TxtOK = "Yes";
            form.TxtCancel = "No";
            form.TxtTitle = "Warning";
            form.TxtMessage = "Do you want to add the subcategory to these categories?";
            form.CurrentCategory = currentCategory;
            parentCategories = new List<Category>();
            foreach (DetailCategory dc in lbCPCParentCategories.Items)
                parentCategories.Add(dc.Category);
            form.ParentCategories = parentCategories;
            form.ShowDialog();
        }

        private void btnCPCCancel_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Close();
        }

        private void btnCPCAdd_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (lbCPCAvailableCategories.SelectedIndex >= 0)
            {
                int index = lbCPCAvailableCategories.SelectedIndex;
                lbCPCParentCategories.Items.Add(lbCPCAvailableCategories.Items[index]);
                lbCPCAvailableCategories.Items.RemoveAt(index);
            }
        }

        private void btnCPCAddAll_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            foreach (DetailCategory dc in lbCPCAvailableCategories.Items)
                lbCPCParentCategories.Items.Add(dc);
            lbCPCAvailableCategories.Items.Clear();
        }

        private void btnCPCRemoveAll_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            foreach (DetailCategory dc in lbCPCParentCategories.Items)
                lbCPCAvailableCategories.Items.Add(dc);
            lbCPCParentCategories.Items.Clear();
        }

        private void btnCPCRemove_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (lbCPCParentCategories.SelectedIndex >= 0)
            {
                int index = lbCPCParentCategories.SelectedIndex;
                lbCPCAvailableCategories.Items.Add(lbCPCParentCategories.Items[index]);
                lbCPCParentCategories.Items.RemoveAt(index);
            }
        }

        private void lbCPCAvailableCategory_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (lbCPCAvailableCategories.SelectedIndex > 0)
            {
                int index = lbCPCAvailableCategories.SelectedIndex;
                lbCPCParentCategories.Items.Add(lbCPCAvailableCategories.Items[index]);
                lbCPCAvailableCategories.Items.RemoveAt(index);
            }
        }

        private void lbCPCPrimaryCategory_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (lbCPCParentCategories.SelectedIndex > 0)
            {
                int index = lbCPCParentCategories.SelectedIndex;
                lbCPCAvailableCategories.Items.Add(lbCPCParentCategories.Items[index]);
                lbCPCParentCategories.Items.RemoveAt(index);
            }
        }
        #endregion


        private void gridChooseParentCategories_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                this.DragMove();
            }
            catch (Exception ex)
            {
            }
        }
	}
}